package com.cq.hd.admin.controller;

import com.cq.hd.admin.annotation.RequiresPermissionsDesc;
import com.cq.hd.admin.biz.OrderBiz;
import com.cq.hd.admin.utils.RegexUtil;
import com.cq.hd.common.enums.biz.ActiveTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.order.api.TbOrderApi;
import com.cq.hd.order.api.dto.*;
import com.cq.hd.order.api.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@Api(tags = "订单相关接口")
@Slf4j
@RestController
@RequestMapping("/order")
public class TbOrderController extends BaseController {

    @Autowired
    private TbOrderApi orderApi;

    @Autowired
    private OrderBiz orderBiz;

    @ApiOperation(value = "获取团购订单的每个订单状态的订单个数")
    @ApiResponse(response = AdminOrderCountVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:groupOrderCount")
    @RequiresPermissionsDesc(menu = {"订单管理", "团购订单管理"}, button = "查询个数")
    @GetMapping("/getGroupOrderCount")
    public Result<List<AdminOrderCountVo>> getGroupOrderCount() {
        AdminOrderCountDto adminOrderCountDto = new AdminOrderCountDto();
        adminOrderCountDto.setOrderType(ActiveTypeEnum.GROUP.getValue());
        return orderApi.getOrderCount(adminOrderCountDto);
    }

    @ApiOperation(value = "获取报名订单的每个订单状态的订单个数")
    @ApiResponse(response = AdminOrderCountVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:enrollOrderCount")
    @RequiresPermissionsDesc(menu = {"订单管理", "报名订单管理"}, button = "查询个数")
    @GetMapping("/getEnrollOrderCount")
    public Result<List<AdminOrderCountVo>> getEnrollOrderCount() {
        AdminOrderCountDto adminOrderCountDto = new AdminOrderCountDto();
        adminOrderCountDto.setOrderType(ActiveTypeEnum.ENROLL.getValue());
        return orderApi.getOrderCount(adminOrderCountDto);
    }

    @ApiOperation(value = "订单列表（包含团购、报名、团购+报名订单）")
    @ApiResponse(response = AdminOrderPageVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:orderList")
    @RequiresPermissionsDesc(menu = {"订单管理", "订单管理"}, button = "查询")
    @GetMapping("/pageOrder")
    public Result<Page<AdminOrderPageVo>> pageOrder(AdminOrderPageDto adminOrderPageDto) {
        adminOrderPageDto.setOrderTypes(
                Arrays.asList(ActiveTypeEnum.GROUP.getValue(), ActiveTypeEnum.ENROLL.getValue(), ActiveTypeEnum.GROUP_ENROLL.getValue()));
        if (adminOrderPageDto.getActiveKeyword() != null) {
            if (StringUtils.isNumeric(adminOrderPageDto.getActiveKeyword())) {
                adminOrderPageDto.setActiveId(Long.valueOf(adminOrderPageDto.getActiveKeyword()));
            } else {
                adminOrderPageDto.setActiveTitle(adminOrderPageDto.getActiveKeyword());
            }
        }
        return orderApi.pageAdminOrder(adminOrderPageDto);
    }

    @ApiOperation(value = "积分订单列表")
    @ApiResponse(response = AdminPointsOrderPageDto.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:orderList")
    @RequiresPermissionsDesc(menu = {"订单管理", "积分订单管理"}, button = "查询")
    @GetMapping("/pagePointsOrder")
    public Result<Page<AdminPointsOrderPageVo>> pagePointsOrder(AdminPointsOrderPageDto adminPointsOrderPageDto) {
        return orderApi.pageAdminPointsOrder(adminPointsOrderPageDto);
    }

    @ApiOperation(value = "导出积分订单")
    @RequiresPermissions("order:order:exportGroupExcel")
    @RequiresPermissionsDesc(menu = {"订单管理", "积分订单管理"}, button = "导出积分订单")
    @GetMapping("/exportPointsExcel")
    public void exportPointsExcel(AdminPointsOrderPageDto adminOrderPageDto, HttpServletResponse response) {
        orderBiz.exportPointsOrderExcel(adminOrderPageDto, response);
    }

    @ApiOperation(value = "佣金订单列表")
    @ApiResponse(response = AdminPointsOrderPageDto.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:orderList")
    @RequiresPermissionsDesc(menu = {"订单管理", "佣金订单"}, button = "查询")
    @GetMapping("/pageCommissionOrder")
    public Result<Page<AdminCommissionOrderPageVo>> pageCommissionOrder(AdminCommissionOrderPageDto adminCommissionOrderPageDto) {
        String activeKeyword = adminCommissionOrderPageDto.getActiveKeyword();
        if (activeKeyword != null && !activeKeyword.isEmpty()) {
            if (StringUtils.isNumeric(activeKeyword)) {
                adminCommissionOrderPageDto.setActiveId(Long.valueOf(activeKeyword));
            } else {
                adminCommissionOrderPageDto.setActiveTitle(activeKeyword);
            }
        }
        return orderApi.pageAdminCommissionOrder(adminCommissionOrderPageDto);
    }

    @ApiOperation(value = "导出佣金订单")
    @RequiresPermissions("order:order:exportGroupExcel")
    @RequiresPermissionsDesc(menu = {"订单管理", "佣金订单管理"}, button = "导出佣金订单")
    @GetMapping("/exportCommissionExcel")
    public void exportCommissionExcel(AdminCommissionOrderPageDto adminOrderPageDto, HttpServletResponse response) {
        String activeKeyword = adminOrderPageDto.getActiveKeyword();
        if (activeKeyword != null && !activeKeyword.isEmpty()) {
            if (StringUtils.isNumeric(activeKeyword)) {
                adminOrderPageDto.setActiveId(Long.valueOf(activeKeyword));
            } else {
                adminOrderPageDto.setActiveTitle(activeKeyword);
            }
        }
        orderBiz.exportCommissionOrderExcel(adminOrderPageDto, response);
    }

    @ApiOperation(value = "订单审核")
    @RequiresPermissions("order:order:orderAudit")
    @RequiresPermissionsDesc(menu = {"订单管理", "订单审核"}, button = "审核")
    @PostMapping("/audit")
    public Result<Void> audit(@RequestBody @Valid OrderAuditDto orderAuditDto) {
        return orderApi.businessOrderAudit(orderAuditDto);
    }

    @ApiOperation(value = "订单批量审核")
    @RequiresPermissions("order:order:orderBatchAudit")
    @RequiresPermissionsDesc(menu = {"订单管理", "订单批量审核"}, button = "审核")
    @PostMapping("/batchAudit")
    public Result<String> batchAudit(@RequestBody @Valid OrderBatchAuditDto orderBatchAuditDto) {
        return orderApi.businessOrderBatchAudit(orderBatchAuditDto);
    }

    @ApiOperation(value = "团购订单列表")
    @ApiResponse(response = AdminOrderPageVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:groupOrderList")
    @RequiresPermissionsDesc(menu = {"订单管理", "团购订单管理"}, button = "查询")
    @GetMapping("/pageGroupOrder")
    public Result<Page<AdminOrderPageVo>> pageGroupOrder(AdminOrderPageDto adminOrderPageDto) {
        adminOrderPageDto.setOrderType(ActiveTypeEnum.GROUP.getValue());
        parseAgentKeyword(adminOrderPageDto);
        return orderApi.pageAdminOrder(adminOrderPageDto);
    }

    private void parseAgentKeyword(AdminOrderPageDto adminOrderPageDto) {
        String agent1Keyword = adminOrderPageDto.getAgent1Keyword();
        String agent2Keyword = adminOrderPageDto.getAgent2Keyword();
        if (agent1Keyword != null) {
            if (RegexUtil.isMobileSimple(agent1Keyword)) {
                adminOrderPageDto.setAgent1Mobile(agent1Keyword);
            } else {
                adminOrderPageDto.setAgent1NickName(agent1Keyword);
            }
        }
        if (agent2Keyword != null) {
            if (RegexUtil.isMobileSimple(agent2Keyword)) {
                adminOrderPageDto.setAgent2Mobile(agent2Keyword);
            } else {
                adminOrderPageDto.setAgent2NickName(agent2Keyword);
            }
        }
    }

    @ApiOperation(value = "报名订单列表")
    @ApiResponse(response = AdminOrderPageVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:enrollOrderList")
    @RequiresPermissionsDesc(menu = {"订单管理", "报名订单管理"}, button = "查询")
    @GetMapping("/pageEnrollOrder")
    public Result<Page<AdminOrderPageVo>> pageEnrollOrder(AdminOrderPageDto adminOrderPageDto) {
        adminOrderPageDto.setOrderType(ActiveTypeEnum.ENROLL.getValue());
        parseAgentKeyword(adminOrderPageDto);
        return orderApi.pageAdminOrder(adminOrderPageDto);
    }

    // 废弃
    @ApiOperation(value = "团购订单详情")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "query", dataType = "String")
    @ApiResponse(response = AdminGroupOrderDetailVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:detailGroupOrder")
    @RequiresPermissionsDesc(menu = {"订单管理", "团购订单管理"}, button = "详情")
    @GetMapping("/detailGroupOrder")
    public Result<AdminGroupOrderDetailVo> detailGroupOrder(Long orderId) {
        return orderApi.detailAdminGroupOrder(orderId);
    }

    // 废弃
    @ApiOperation(value = "报名订单详情")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "query", dataType = "String")
    @ApiResponse(response = AdminEnrollOrderDetailVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:detailEnrollOrder")
    @RequiresPermissionsDesc(menu = {"订单管理", "报名订单管理"}, button = "详情")
    @GetMapping("/detailEnrollOrder")
    public Result<AdminEnrollOrderDetailVo> detailEnrollOrder(Long orderId) {
        return orderApi.detailAdminEnrollOrder(orderId);
    }

    @ApiOperation(value = "订单详情")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "query", dataType = "String")
    @ApiResponse(response = AdminOrderDetailVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("order:order:detailOrder")
    @RequiresPermissionsDesc(menu = {"订单管理", "订单管理"}, button = "详情")
    @GetMapping("/detailOrder")
    public Result<AdminOrderDetailVo> detailOrder(Long orderId) {
        return orderApi.detailAdminOrder(orderId);
    }

    @ApiOperation(value = "导出团购订单")
    @RequiresPermissions("order:order:exportGroupExcel")
    @RequiresPermissionsDesc(menu = {"订单管理", "报名订单管理"}, button = "导出团购订单")
    @GetMapping("/exportGroupExcel")
    public void exportGroupExcel(AdminOrderPageDto adminOrderPageDto, HttpServletResponse response) {
        adminOrderPageDto.setOrderType(ActiveTypeEnum.GROUP.getValue());
        orderBiz.exportGroupOrderExcel(adminOrderPageDto, response);
    }

    @ApiOperation(value = "导出报名订单")
    @RequiresPermissions("order:order:exportEnrollExcel")
    @RequiresPermissionsDesc(menu = {"订单管理", "报名订单管理"}, button = "导出报名订单")
    @GetMapping("/exportEnrollExcel")
    public void exportEnrollExcel(AdminOrderPageDto adminOrderPageDto, HttpServletResponse response) {
        adminOrderPageDto.setOrderType(ActiveTypeEnum.ENROLL.getValue());
        orderBiz.exportEnrollOrderExcel(adminOrderPageDto, response);
    }
}
